package mark.tom.sail;

import android.content.Context;

import androidx.annotation.NonNull;

public class SailBuilder {

    private final Context mContext;
    private final String mIdKey;
    private int maxRecordLimit = 300;
    private DataConverter converter;
    private DataSecurity security;
    private FileGenerator fileGenerator;
    private FileStorage fileStorage;
    private RecordUploader recordUploader;
    private RecordCollector recordCollector;
    private LogInterceptor logInterceptor;

    SailBuilder(@NonNull Context context) {
        this.mContext = context.getApplicationContext();
        this.mIdKey = String.valueOf(System.currentTimeMillis());
    }

    public SailBuilder setConverter(@NonNull DataConverter converter) {
        this.converter = converter;
        return this;
    }

    public SailBuilder setSecurity(@NonNull DataSecurity security) {
        this.security = security;
        return this;
    }

    public SailBuilder setFileGenerator(@NonNull FileGenerator fileGenerator) {
        this.fileGenerator = fileGenerator;
        return this;
    }

    public SailBuilder setMaxRecordLimit(int maxRecordLimit) {
        if (maxRecordLimit <= 0) {
            throw new IllegalArgumentException("maxRecordLimit must > 0");
        }
        this.maxRecordLimit = maxRecordLimit;
        return this;
    }

    public SailBuilder setFileStorage(@NonNull FileStorage fileStorage) {
        this.fileStorage = fileStorage;
        return this;
    }

    public SailBuilder setRecordUploader(@NonNull RecordUploader recordUploader) {
        this.recordUploader = recordUploader;
        return this;
    }

    public SailBuilder setRecordCollector(@NonNull RecordCollector recordCollector) {
        this.recordCollector = recordCollector;
        return this;
    }

    public SailBuilder setLogInterceptor(@NonNull LogInterceptor logInterceptor) {
        this.logInterceptor = logInterceptor;
        return this;
    }

    private DataConverter converter() {
        return converter;
    }

    private DataSecurity security() {
        if (null == security) {
            security = new Base64DataSecurity(logInterceptor());
        }
        return security;
    }

    private FileGenerator fileGenerator() {
        if (null == fileGenerator) {
            fileGenerator = new DefaultFileGenerator(maxRecordLimit);
        }
        return fileGenerator;
    }

    FileStorage fileStorage() {
        if (null == fileStorage) {
            fileStorage = new DefaultFileStorage(mContext, logInterceptor());
        }
        return fileStorage;
    }

    LogInterceptor logInterceptor() {
        if (null == logInterceptor) {
            logInterceptor = new LogInterceptor.EmptyLogInterceptor();
        }
        return logInterceptor;
    }

    AnalyticsCollector analyticsCollector() {
        return new DefaultAnalyticsCollector(mIdKey, converter(), fileGenerator(), fileStorage(), security(), logInterceptor(), recordCollector);
    }

    AnalyticsReporter analyticsReporter() {
        return new DefaultAnalyticsReporter(mIdKey, converter(), fileGenerator(), fileStorage(), security(), logInterceptor(), recordUploader);
    }

    public void build() {
        Sail.build(this);
    }
}
